/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.editors;

import java.lang.Short;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.zapta.apps.maniana.R;
import com.zapta.apps.maniana.annotations.MainActivityScope;
import com.zapta.apps.maniana.editors.EditRepeat;

/*
**
** @author Elliott Mitchell
**/
@MainActivityScope
public class EditRepeatDay extends EditRepeat
implements CompoundButton.OnCheckedChangeListener {

	@Override
	public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle saved) {
		final View ret=super.onCreateView(inflator, container, saved);

		final ToggleButton toggle=(ToggleButton)ret.findViewById(
R.id.editor_repeat_sched_type);
		toggle.setChecked(getArguments().
getBoolean("item_repeat_day_fixed", false));
		toggle.setOnCheckedChangeListener(this);

		// clear state from other repeat types
		getArguments().remove("item_repeat_relative_end");
		getArguments().remove("item_repeat_day_of_week");
		getArguments().remove("item_repeat_months");

		return ret;
	}

	@Override
	public void setArguments(Bundle bundle) {
		super.setArguments(bundle, "day");
	}

	@Override
	int getLayout() {
		return R.layout.editor_repeat_day;
	}

	@Override
	int getRepeatHeaderId() {
		return R.string.editor_repeat_day_header;
	}

	public void onCheckedChanged(CompoundButton but, boolean st) {
		getArguments().putBoolean("item_repeat_day_fixed", st);
	}

	@Override
	int getRepeatPeriod() {
		return (getArguments().getShort("item_repeat_days", (short)0)&0xFFFF)+1;
	}

	@Override
	int setRepeatPeriod(int count) {
		if(count>0x10000) count=0x10000; // max value
		getArguments().putShort("item_repeat_days", (short)(count-1));
		return count;
	}
}

